/**
*LeftNavView Page class
*auth:lww
*version:1.0
*since:2014-10-11
*@param:context:Application
*@param:router:Router
*/
function LeftNavView(context,router,page) {
    this.id = "";
    this.name = "LeftNavView";
    this.version = "1.0.0";
    this.mContext = context;
    this.mRouter = router;   
    this.mPage = page;
    this.mRootViewObject = null; 
    this.titleLayer = null; 
    this.menuLayer = null; 
    this.mTitleView = null;
    this.mFoldBtnView = null;
    this.mTreeMenuView = null;
    this.folded = false; 
    this.fullScreen = false;
}
LeftNavView.prototype.getId = function () {
    if (this.id == null || this.id == "") {
        this.id = id();
    }
    return this.id;
};
LeftNavView.prototype.setId = function (ids) {
    this.id = ids;
};
LeftNavView.prototype.isFolded = function () {
    return this.folded;
};
LeftNavView.prototype.isFullScreen = function () {
    return this.fullScreen;
};
LeftNavView.prototype.view = function () {
    return this.invalidate();
};
LeftNavView.prototype.invalidate = function () {    
    if (this.mRootViewObject == null) {
        this.mRootViewObject = new LinearLayout(this.mContext);
        this.mRootViewObject.setLayoutParams(new LayoutParams(MATCH_PARENT, MATCH_PARENT));
        this.mRootViewObject.setBackground(new Drawable("#6599a2",null));
        this.mRootViewObject.view().style.overflow = "hidden";
        this.initView();
    }
    return this.mRootViewObject.view();
};
LeftNavView.prototype.initView = function () {
    this.titleLayer = new LinearLayout(this.mContext);
    this.titleLayer.setLayoutParams(new LayoutParams(MATCH_PARENT, 28));
    this.titleLayer.setBackground(new Drawable("#3E77AC", null));
    this.mRootViewObject.setView(this.titleLayer.view());
    this.menuLayer = new LinearLayout(this.mContext); 
    this.menuLayer.setLayoutParams(new LayoutParams(MATCH_PARENT, MATCH_PARENT)); 
    this.mRootViewObject.addChildView(this.menuLayer.view());
    this.updateTitleView();
    this.updateMenuView();
};
LeftNavView.prototype.updateTitleView = function () {
    if(isNull(this.mRouter.currentMainNavMenuInfo)){
        return;
    }  
    if(this.mRouter.currentMainNavMenuInfo.isLeaf == 1){
        this.fullScreen = true;
    }
    var leftLayer = new LinearLayout(this.mContext);
    leftLayer.setLayoutParams(new LayoutParams(MATCH_PARENT, MATCH_PARENT)); 
    var rightLayer = new LinearLayout(this.mContext);
    rightLayer.setLayoutParams(new LayoutParams(31, MATCH_PARENT));
    setCssFloat(rightLayer.view(), "right");   
    this.titleLayer.addChildView(rightLayer.view());
    this.titleLayer.addChildView(leftLayer.view());
    this.mTitleView = formatHtmlString(this.mRouter.currentMainNavMenuInfo.getName() ,"#ffffff", "13px", "bold", "2px 0 0 6px");
    this.mFoldBtnView = createButton(this.makeFoldBtnViewChild(), function(othis){ return othis.foldMenuView();}, this,"foldBtn");
    leftLayer.setView(this.mTitleView);
    rightLayer.setView(this.mFoldBtnView);
};
LeftNavView.prototype.foldMenuView = function () {
    this.folded=this.folded?false:true;
    this.onMenuViewFold(this.folded,this.mPage);
    setChild(this.mFoldBtnView,this.makeFoldBtnViewChild());
    showEle(this.mTitleView, this.folded?"none":"inline-block");    
    showEle(this.mTreeMenuView.view(), this.folded?"none":"block");
    return this.folded;
};
LeftNavView.prototype.makeFoldBtnViewChild = function(){
    return formatHtmlString(this.folded?"»":"‹","#ffffff", "13px", "bold", "2px 0 0 2px");
};
LeftNavView.prototype.updateMenuView = function () {
    if(isNull(this.mRouter.currentMainNavMenuInfo)){
        return;
    }
    this.mTreeMenuView = this.initTreeMenuView("treeMenu");         
    this.parseMenuList(this.mTreeMenuView, this.mRouter.currentMainNavMenuInfo);        
    this.menuLayer.setView(this.mTreeMenuView.view());
    if(this.isFullScreen()){
        this.onMenuEvent(this.mRouter.currentMainNavMenuInfo,this.mPage);
    }
};
LeftNavView.prototype.initTreeMenuView = function (mainStyleName) {   
    var theView = this;
    var mTreeMenuView = new TreeView();
    mTreeMenuView.setClassName(mainStyleName);
    mTreeMenuView.onSelected = function (item) {
        if (!isNull(item)) { //menu selected event   
            theView.onMenuEvent(item.getDataSource(),theView.mPage);                         
        }
    } 
    return mTreeMenuView;
};
LeftNavView.prototype.parseMenuList = function(mTree, menuInfo){
    if (typeof (menuInfo) == 'undefined'
        || isNull(menuInfo)
        || menuInfo.getItemCount() <= 0) {
        return;
    }
    var othis = this;
    for (var i = 0; i < menuInfo.getItemCount(); i++) {
        var menuItemInfo = menuInfo.getItem(i);
        //new menu item view
        var item = new TreeViewItem(mTree, menuItemInfo.getName());
        item.setDataSource(menuItemInfo);
        item.setClassName("treeMenuItem");
        item.setTitleClassName("treeMenuItemTitle");       
        item.setExpandIco("./stylesheets/ico/item.png");
        item.setUnExpandIco("./stylesheets/ico/item.png");
        item.setExpandRightIco("./stylesheets/ico/arrow_down.png");
        item.setUnExpandRightIco("./stylesheets/ico/arrow_left.png");
        item.setIco("./stylesheets/ico/item.png");
        item.setMenuStyle(true);
        if (mTree.getLevel() == 0) {
            item.addTitleClassName("treeMenuItemTitleBg");
        }
        mTree.addItem(item);
        //parse child menu
        if (menuItemInfo.getItemCount() > 0) {
            var childTree = new TreeView(item);
            childTree.setClassName("treeMenu");
            childTree.setLevel(item.getLevel() + 1);
            //call func
            othis.parseMenuList(childTree, menuItemInfo);
            item.setChildTree(childTree);
            //default unExpand
            item.unExpandChildTree();
        }
    }
}; 
LeftNavView.prototype.onMenuViewFold = function (folded,thePage) {console.log("onMenuViewFold:".concat(folded));};
LeftNavView.prototype.onMenuEvent = function (menuItem,thePage) { console.log("onMenuEvent:");};

markscript("LeftNavView");
//end LeftNavView.js